﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NewsInformationTypeActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The news information type actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Activities
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using NHibernate;

    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Commands;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The news information type actioner.
    /// </summary>
    internal class NewsInformationTypeActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="NewsInformationTypeActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The session.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public NewsInformationTypeActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create news information type.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <returns>
        /// The <see cref="Guid"/>.
        /// </returns>
        public Guid CreateNewsInformationType(NewsInformationType obj)
        {
            this.Session.Save(obj);

            // log.CreateLogEntry(obj.ID, "objekt '{0}' blev oprettet.", Models.Base.LogEntryType.NewsInformationType);
            return obj.ID;
        }

        /// <summary>
        /// The create news information type.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        public void CreateNewsInformationType(CreateNewsInformationTypeCommand cmd)
        {
            if (cmd.Dto != null)
            {
                var obj = new NewsInformationType { ID = cmd.Dto.ID, Name = cmd.Dto.Name };
                CreateNewsInformationType(obj);
            }
        }

        /// <summary>
        /// The delete news information type.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        public void DeleteNewsInformationType(DeleteNewsInformationTypeCommand cmd)
        {
            if (cmd.Dto != Guid.Empty)
            {
                if (this.Session.QueryOver<NewsInformation>().Where(x => x.Type.ID == cmd.Dto).RowCount() == 0)
                {
                    var type = this.Session.Get<NewsInformationType>(cmd.Dto);
                    if (type != null)
                    {
                        this.Session.Delete(type);
                    }
                }
            }
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="newsInformationTypeQuery">
        /// The news information type query.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public IResult ExecuteSearchQuery(NewsInformationTypeQuery newsInformationTypeQuery)
        {
            SearchSettings settings = newsInformationTypeQuery.Settings;
            var result = new NewsInformationTypeResult();

            IQueryOver<NewsInformationType, NewsInformationType> search = this.Session.QueryOver<NewsInformationType>();
            this.ApplyDefaultCriterias(newsInformationTypeQuery.Settings, search);

            result.TotalRowCount = search.RowCount();
            IList<NewsInformationType> list = ApplySkipAndTake(settings, search);

            if (list.Any())
            {
                list = list.OrderBy(x => x.Name).ToList();
                foreach (NewsInformationType newsInformationType in list)
                {
                    result.List.Add(Services.Dto.NewsInformationType.Convert(newsInformationType));
                }
            }

            return result;
        }

        /// <summary>
        /// The update news information type.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        public void UpdateNewsInformationType(UpdateNewsInformationTypeCommand cmd)
        {
            if (cmd.Dto != null)
            {
                var obj = this.Session.Get<NewsInformationType>(cmd.Dto.ID);
                if (obj != null)
                {
                    obj.Name = cmd.Dto.Name;
                    this.Session.Update(obj);
                }
            }
        }

        #endregion
    }
}